﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using App.Common;
using QxFramework.Core;
using System;
public class Portal : SceneMonoBase
{
    [HideInInspector]
    public bool IsPlayerIn;
    [HideInInspector]
    public bool IaActivated;
    [HideInInspector]
    public int CurrentPlayerID;

    public override void Awake()
    {
        Alph = 0;
        GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, Alph);
        base.Awake();
    }
    public override void Update()
    {
        if (!GameMgr.Get<ISceneMonoManager>().GetAllSceneMono()._sceneMonoDatas.ContainsKey(gameObject.name))
        {
            return;
        }
        CheckPlayerIsIn();
        CheckActivated();
        CheckPort();
        base.Update();
    }

    float Alph = 0;
    float AlphPlus = 0;
    private void CheckActivated()
    {
        IaActivated = GameMgr.Get<IMapManager>().CheckClear(false);
        if (IaActivated)
        {
            if (Alph > 1f)
            {
                Alph = 1;
                AlphPlus = -0.025f;
            }
            if (Alph < 0.5f)
            {
                AlphPlus = 0.025f;
            }
            if (IsPlayerIn)
            {
                AlphPlus = 0.1f;
            }
            Alph += AlphPlus;
        }
        else
        {
            if (Alph < 0)
            {
                Alph = 0;
            }
            else
            {
                Alph -= Time.deltaTime;
            }
        }
        GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, Alph);
    }
    public void CheckPort()
    {
        if(IaActivated && IsPlayerIn && InputManager.Instance.Port(CurrentPlayerID))
        {
            UIManager.Instance.Open("BlackMask", 2, args: new BlackMask.BlackMaskArg
                (() => { GameMgr.Get<IMapManager>().ToTheNextRoom(
                    () => { UIManager.Instance.GetUI("BlackMask").GetComponent<BlackMask>().CloseUI();
                    }); }));
        }
    }

    private void CheckPlayerIsIn()
    {
        IsPlayerIn = false;
        CurrentPlayerID = -1;
//         foreach (GameObject obj in GameMgr.Get<IMonoManager>().GetAllMono()._ObjList.Values)
//         {
//             if (obj.tag == "Player")
//             {
//                 if ((sceneMonoData.shape == Shape.Square
//                     && Mathf.Abs(GameMgr.Get<IMonoManager>().GetMono(obj.name).Position.x - sceneMonoData.position.x) < sceneMonoData.size.x / 2
//                     && Mathf.Abs(GameMgr.Get<IMonoManager>().GetMono(obj.name).Position.y - sceneMonoData.position.y) < sceneMonoData.size.y / 2)
//                     || (sceneMonoData.shape == Shape.Circle
//                     && (GameMgr.Get<IMonoManager>().GetMono(obj.name).Position - sceneMonoData.position).magnitude < sceneMonoData.size.x
//                     ))
//                 {
//                     CurrentPlayerID = obj.GetComponent<Player>().PlayerID;
//                     IsPlayerIn = true;
//                 }
//             }
//         }
    }
    public override void OnDrawGizmos()
    {
        Gizmos.color = new Color(0, 0, 0, 0.5f);
        base.OnDrawGizmos();
    }
}
